package br.edu.fa7.receita.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.DatePicker;
import br.edu.fa7.receita.ui.CadastroUsuarioActivity;

@SuppressLint("ValidFragment")
public class DatePickerFragment extends DialogFragment implements OnDateSetListener {
	
	private CadastroUsuarioActivity cadastroUsuarioActivity;
	
	public DatePickerFragment(CadastroUsuarioActivity activity) {
		this.cadastroUsuarioActivity = activity;
	}
	
	@SuppressLint("SimpleDateFormat")
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		Calendar calendar = Calendar.getInstance();
		
		int dia = calendar.get(Calendar.DAY_OF_MONTH);
		int mes = calendar.get(Calendar.MONTH);
		int ano = calendar.get(Calendar.YEAR);
		
		final DatePickerDialog dialogo = new DatePickerDialog(getActivity(), DatePickerDialog.THEME_TRADITIONAL, this, ano, mes, dia);
		
		DialogInterface.OnClickListener dialogConfirmar = new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
				
				Calendar calendario = Calendar.getInstance();
				calendario.set(Calendar.DAY_OF_MONTH, dialogo.getDatePicker().getDayOfMonth());
				calendario.set(Calendar.MONTH, dialogo.getDatePicker().getMonth() + 1);
				calendario.set(Calendar.YEAR, dialogo.getDatePicker().getYear());
				
				String data = df.format(calendario.getTime());
				
				cadastroUsuarioActivity.getDataNascimento().setText(data);
			}
		};
		
		dialogo.setButton(DatePickerDialog.BUTTON_POSITIVE, "Confirmar", dialogConfirmar);
		
		DialogInterface.OnClickListener dialogCancelar = new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialogo.dismiss();
			}
		};
		
		dialogo.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Cancelar", dialogCancelar);
		
		return dialogo;
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {	}

}
